package com.finger.forest;

import android.annotation.SuppressLint;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.finger.forest.base.BaseActivity;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.utils.ToastUtils;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;

/**
 * 查看视频
 */
public class CheckLookVideoActivity extends BaseActivity<RegisterPresenter> {

    private static final String TAG = "CheckLookVideoActivity";

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.ivTop)
    ImageView ivTop;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.ivBottom)
    ImageView ivBottom;
    @BindView(R.id.ivReset)
    ImageView ivReset;
    @BindView(R.id.ivPlay)
    ImageView ivPlay;
    @BindView(R.id.ivBack)
    ImageView ivBack;

    @BindView(R.id.tvReset)
    TextView tvReset;
    @BindView(R.id.tvZoom)
    TextView tvZoom;
    @BindView(R.id.tvFocusing)
    TextView tvFocusing;
    @BindView(R.id.tvAperture)
    TextView tvAperture;

    @BindView(R.id.web_view_vedio)
    WebView vedioWebView;

    @Override
    protected int setView() {
        return R.layout.activity_check_look_video;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {
        vedioWebView.setWebViewClient(new WebViewClient());

        WebSettings setting = vedioWebView.getSettings();
        setting.setUseWideViewPort(true);
        setting.setLoadWithOverviewMode(true);
        // 设置与Js交互的权限
        setting.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        setting.setJavaScriptCanOpenWindowsAutomatically(true);
        //自适应屏幕
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        setting.setLoadWithOverviewMode(true);

        String URL = "file:///android_asset/index.html";
        // 先载入JS代码
        // 格式规定为:file:///android_asset/文件名.html
        vedioWebView.loadUrl(URL);

        clicks();
    }

    @SuppressLint("WrongConstant")
    private void clicks() {
        //左边按钮
        RxView.clicks(ivLeft).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("左边按钮");
        });

        //上边按钮
        RxView.clicks(ivTop).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("上边按钮");
        });

        //右边按钮
        RxView.clicks(ivRight).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("右边按钮");
        });

        //下边按钮
        RxView.clicks(ivBottom).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("下边按钮");
        });

        //复位
        RxView.clicks(ivReset).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("复位");
        });

        //复位
        RxView.clicks(tvReset).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("复位");
        });

        //变焦
        RxView.clicks(tvZoom).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("变焦");
        });

        //聚焦
        RxView.clicks(tvFocusing).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("聚焦");
        });

        //光圈
        RxView.clicks(tvAperture).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            ToastUtils.show("光圈");
        });

        //播放
        RxView.clicks(ivPlay).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            //隐藏播放按钮
            ivPlay.setVisibility(View.INVISIBLE);
            // 通过Handler发送消息，调js播放方法
            vedioWebView.post(new Runnable() {
                @Override
                public void run() {

                    // 注意调用的JS方法名要对应上
                    // 调用javascript的callJS()方法
                    vedioWebView.loadUrl("javascript:playUrl()");
                }
            });
        });

        //返回
        RxView.clicks(ivBack).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            finish();
        });
    }

    @Override
    protected void loadDataFromServer() {

    }
}